package com.hxzy.controller;

import com.alibaba.fastjson.JSONObject;
import com.hxzy.util.BeanValidatorUtil;
import com.hxzy.util.RedisUtil;
import com.hxzy.common.BaseServlet;
import com.hxzy.common.ResultAjax;
import com.hxzy.dto.LoginDTO;
import com.hxzy.entity.UserInfo;
import com.hxzy.service.UserInfoService;
import com.hxzy.service.impl.UserInfoServiceImpl;
import org.mindrot.jbcrypt.BCrypt;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * @Description:
 * @Author: ykj
 * @Date: 2022/3/3 23:14
 */
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends BaseServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //收集参数
        LoginDTO loginDTO = super.CollectiongParameterToJSON(req, LoginDTO.class);
        //验证
        Set<String> validator = BeanValidatorUtil.validator(LoginDTO.class, loginDTO);
        if (validator.isEmpty()){
            //获取验证码
            String uuid ="captcha:"+loginDTO.getUuid();
            //获取值
            String redisValue = getRedis(uuid);
            //判读验证码是否存在 存在->删除
            if (redisValue!=null){
                delRRedis(uuid);
            }

            if (null == redisValue){
                ResultAjax error = ResultAjax.error("验证码过期，请重新刷新");
                super.render(resp, JSONObject.toJSONString(error));
                return;
            }

            //验证码是否正确
            if (!redisValue.equals(loginDTO.getCode())){
                ResultAjax error = ResultAjax.error("验证码不正确");
                super.render(resp, JSONObject.toJSONString(error));
                return;
            }

            //数据库查找
            UserInfoService service=new UserInfoServiceImpl();
            UserInfo userInfo = service.searchByName(loginDTO.getLoginName());
            if (userInfo==null){
                ResultAjax error = ResultAjax.error("账号或者密码不正确");
                super.render(resp, JSONObject.toJSONString(error));
                return;
            }

            //验证密码，用第三方库
            boolean checkpw = BCrypt.checkpw(loginDTO.getLoginPwd(), userInfo.getLoginPwd());
            if (!checkpw){
                ResultAjax error = ResultAjax.error("密码错误");
                super.render(resp, JSONObject.toJSONString(error));
                return;
            }
            //验证状态
            if (userInfo.getStatus().equals(1)){
                ResultAjax error = ResultAjax.error("账号被冻结");
                super.render(resp, JSONObject.toJSONString(error));
            }

            String id=req.getSession().getId();
            String redisKey="login_total:"+id;
            String setRedisValue=JSONObject.toJSONString(userInfo);
            long seconds=36000;
            setRedis(redisKey, seconds, setRedisValue);
            ResultAjax success = ResultAjax.success(id);
            super.render(resp, JSONObject.toJSONString(success));
        }else {
            ResultAjax error = ResultAjax.error(validator.iterator().next());
            super.render(resp, JSONObject.toJSONString(error));
        }

    }

    //获取键对应的值
    private String getRedis(String redisKey){
        Jedis jedis = RedisUtil.create();
        return jedis.get(redisKey);
    }

    private long delRRedis(String redisKey){
        Jedis jedis = RedisUtil.create();
        return jedis.del(redisKey);
    }
    private String setRedis(String redisKey,long seconds,String rediskeyValue){
        Jedis jedis = RedisUtil.create();
        return jedis.setex(redisKey, seconds, rediskeyValue);
    }

}
